package com.example.administrator.demob;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

public class StuAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Student> studentArrayList;
    private InOnDataChangeListener listener;

    public StuAdapter(Context context, ArrayList<Student> studentArrayList,
                      InOnDataChangeListener listener) {
        this.context = context;
        this.studentArrayList = studentArrayList;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return studentArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return studentArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if(view == null){
            view = LayoutInflater.from(context).inflate(R.layout.stu_item,viewGroup,false);

            StuViewHolder viewHolder = new StuViewHolder();
            viewHolder.txtStuNo = view.findViewById(R.id.txt_stuNo);
            viewHolder.txtName = view.findViewById(R.id.txt_name);
            viewHolder.txtAge = view.findViewById(R.id.txt_age);
            viewHolder.ivDel = view.findViewById(R.id.iv_del);

            view.setTag(viewHolder);
        }

        StuViewHolder holder = (StuViewHolder) view.getTag();
        final Student s = studentArrayList.get(i);
        holder.txtStuNo.setText(s.getStuNo());
        holder.txtName.setText(s.getName());
        holder.txtAge.setText(String.valueOf(s.getAge()));

        holder.ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.del(s);
            }
        });

        return view;
    }
}
